#include "timer2.h"
#include "gd32f4xx.h"
#include "systick.h"
#if USE_TIMER2

void timer2_init() {
    /**
     * 所用功能共用的部分 不会改变的部分
     * 注意 同一个定时器 同一时间 不同通道
     * pwm输出的周期 频率 都是一致的
     * 但是 每个通道的duty可以独立设置
     */
    uint32_t timer_rcu = RCU_TIMER2;                                     // 时钟信号
    uint32_t timer = TIMER2;                                             //  哪个timer
    uint32_t timer_param_psc = TIMER2_PSC;                         // 预分频系数
    uint32_t timer_param_period = TIMER2_PERIOD; // 设置周期计数
    /************************ timer 通用配置 ************************/
    rcu_timer_clock_prescaler_config(RCU_TIMER_PSC_MUL4); // 全局倍频操作
    // 初始化定时器相关配置
    rcu_periph_clock_enable(timer_rcu); // 初始化时钟信号
    timer_deinit(timer);                // 初始化timer
    timer_parameter_struct tps;
    timer_struct_para_init(&tps);    // 配置默认值
    tps.prescaler = timer_param_psc; // 根据实际需求去修改值
    tps.period = timer_param_period; // 周期计数（从0开始计数）
    timer_init(timer, &tps);
    /************************ timer 通用配置结束 ************************/

    /**************************GPIO配置开始 ***************************/
#if TIMER2_CH0_ENABLE
    // gpio初始化 ch0
    rcu_periph_clock_enable(TIMER2_CH0_PORT_RCU);
    gpio_mode_set(TIMER2_CH0_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER2_CH0_PORT_PIN);
    gpio_output_options_set(TIMER2_CH0_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER2_CH0_PORT_PIN);
    gpio_af_set(TIMER2_CH0_PORT, TIMER2_AF, TIMER2_CH0_PORT_PIN);
#endif

#if TIMER2_CH1_ENABLE
    // gpio初始化 ch1
    rcu_periph_clock_enable(TIMER2_CH1_PORT_RCU);
    gpio_mode_set(TIMER2_CH1_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER2_CH1_PORT_PIN);
    gpio_output_options_set(TIMER2_CH1_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER2_CH1_PORT_PIN);
    gpio_af_set(TIMER2_CH1_PORT, TIMER2_AF, TIMER2_CH1_PORT_PIN);
#endif

#if TIMER2_CH2_ENABLE
    // gpio初始化 ch2
    rcu_periph_clock_enable(TIMER2_CH2_PORT_RCU);
    gpio_mode_set(TIMER2_CH2_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER2_CH2_PORT_PIN);
    gpio_output_options_set(TIMER2_CH2_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER2_CH2_PORT_PIN);
    gpio_af_set(TIMER2_CH2_PORT, TIMER2_AF, TIMER2_CH2_PORT_PIN);
#endif

#if TIMER2_CH3_ENABLE
    // gpio初始化 ch3
    rcu_periph_clock_enable(TIMER2_CH3_PORT_RCU);
    gpio_mode_set(TIMER2_CH3_PORT, GPIO_MODE_AF, GPIO_PUPD_NONE, TIMER2_CH3_PORT_PIN);
    gpio_output_options_set(TIMER2_CH3_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_MAX, TIMER2_CH3_PORT_PIN);
    gpio_af_set(TIMER2_CH3_PORT, TIMER2_AF, TIMER2_CH3_PORT_PIN);
#endif
    /************************GPIO配置结束***************************/
    /************************CH配置开始***************************/
    timer_oc_parameter_struct tops;
    // 定时器通道配置  CH0
#if TIMER2_CH0_ENABLE
    timer_channel_output_struct_para_init(&tops);
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_0, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_0, TIMER_OC_MODE_PWM0); // 
    timer_channel_output_pulse_value_config(timer, TIMER_CH_0, 0); // pwm 初始duty =0
#endif

#if TIMER2_CH1_ENABLE
    timer_channel_output_struct_para_init(&tops);
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_1, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_1, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_1, 0); // pwm值 初始duty =0
#endif
#if TIMER2_CH2_ENABLE
    timer_channel_output_struct_para_init(&tops);
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_2, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_2, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_2, 0); // pwm值 初始duty =0
#endif

#if TIMER2_CH3_ENABLE
    timer_channel_output_struct_para_init(&tops);
    tops.outputstate = (uint16_t)TIMER_CCX_ENABLE;
    timer_channel_output_config(timer, TIMER_CH_3, &tops);
    timer_channel_output_mode_config(timer, TIMER_CH_3, TIMER_OC_MODE_PWM0);
    timer_channel_output_pulse_value_config(timer, TIMER_CH_3, 0); // pwm值 初始duty =0
#endif
    /************************CH配置结束***************************/
    timer_enable(timer); // 开启定时器
}

/**********************向外提供修改占空比的函数开始****************/
#if TIMER2_CH0_ENABLE
void timer2_ch0_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER2_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_0, pulse); // pwm值
}
#endif

#if TIMER2_CH1_ENABLE
void timer2_ch1_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER2_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_1, pulse); // pwm值
}
#endif
#if TIMER2_CH2_ENABLE
void timer2_ch2_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER2_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_2, pulse); // pwm值
}
#endif
#if TIMER2_CH3_ENABLE
void timer2_ch3_update(float duty) {
    if (duty > 100)
        duty = 100;
    if (duty < 0)
        duty = 0;
    // uint32_t  timer_param_period = TIMER2_PERIOD; 设置频率
    uint32_t count = TIMER2_PERIOD + 1;
    uint32_t pulse = count * duty / 100 - 1;
    // 计数值
    timer_channel_output_pulse_value_config(TIMER2, TIMER_CH_3, pulse); // pwm值
}
#endif
/**********************向外提供修改占空比的函数结束****************/
#endif